package com.hdaccp.dao.impl;

import com.hdaccp.dao.CommonDao;
import com.hdaccp.dao.SopDao;
import com.hdaccp.entity.Sop;
import com.hdaccp.entity.Status;
import com.hdaccp.util.BaseDao;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SopDaoimpl extends BaseDao implements CommonDao,SopDao {
    @Override
    public List getAll() {
        return null;
    }

    @Override
    public Object getById(Serializable id) {
        return null;
    }

    @Override
    public boolean add(Object entity) {
        return false;
    }

    @Override
    public boolean update(Object entity) {
        return false;
    }

    @Override
    public boolean deleteById(Serializable id) {
        return false;
    }

	@Override
	public List<Sop> getYAll(int zid) {
		// TODO Auto-generated method stub
		String sql="select * from sop where zid=? order by `time`";
		Object [] params={zid};
		ResultSet rs=super.executeQuery(sql, params);
		List<Sop> list=new ArrayList<Sop>();
		try {
			while(rs.next()){
				Sop s=new Sop();
				s.setId(rs.getInt(1));
				s.setTime(rs.getDate(2).toString());
				s.setStatus(rs.getString(3));
				s.setProblem(rs.getString(4));
				s.setZid(rs.getInt(5));
				list.add(s);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public boolean Yins(Sop s) {
		// TODO Auto-generated method stub
		String sql="insert into sop values(null,NOW(),?,?,?)";
		Object [] params={s.getStatus(),s.getProblem(),s.getZid()};
		return super.executeUpdate(sql, params);
	}

	@Override
	public boolean Yupd(Sop s) {
		// TODO Auto-generated method stub
		String sql="update sop set  `status`=?,problem=? where id=?";
		Object [] params={s.getStatus(),s.getProblem(),s.getId()};
		return super.executeUpdate(sql, params);
	}

	@Override
	public Sop getYID(int zid) {
		String sql="select * from sop where zid=? order by `time` desc limit 0,1";
		Object [] params={zid};
		ResultSet rs=super.executeQuery(sql, params);
		Sop s=null;
		try {
			while(rs.next()){
				s=new Sop();
				s.setId(rs.getInt(1));
				s.setTime(rs.getDate(2).toString());
				s.setStatus(rs.getString(3));
				s.setProblem(rs.getString(4));
				s.setZid(rs.getInt(5));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return s;
	}

	@Override
	public Sop getYID() {
		String sql="select * from `sop` order by id desc limit 0,1";
		Object [] params=null;
		ResultSet rs=super.executeQuery(sql, params);
		Sop s=null;
		try {
			while(rs.next()){
				s=new Sop();
				s.setId(rs.getInt(1));
				s.setTime(rs.getDate(2).toString());
				s.setStatus(rs.getString(3));
				s.setProblem(rs.getString(4));
				s.setZid(rs.getInt(5));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return s;
	}

	@Override
	public int getYCha(int zid, String status) {
		// TODO Auto-generated method stub
		String sql="select COUNT(*) from sop where zid=? and `status`=?";
		Object [] params={zid,status};
		ResultSet rs=super.executeQuery(sql, params);
		int count=0;
		try {
			while(rs.next()){
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				super.closeAll(rs, rs.getStatement(), rs.getStatement().getConnection());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return count;
	}
}
